import redis

class Myredis:
    def __init__(self) -> None:
        self.r = redis.Redis(password='021327')

    # 字符串添加
    def str_add(self, key, value):
        self.r.set(key, value)

    # 加入过期时间添加
    def str_setex(self, key, value, time):
        self.r.setex(key, time, value)


    def str_setnx(self,key,value):
        return self.r.setnx(key,value)


    # 字符串获取
    def str_get(self, key):
        return self.r.get(key)

    # 删除key
    def del_key(self, key):
        self.r.delete(key)

    # 加入队列lpush  rlush
    def list_lpush(self, key, value):
        self.r.lpush(key, value)
    def list_rpush(self, key, value):
        self.r.rpush(key, value)
    # 获取单条数据 lpop rpop
    def list_rpop(self, key):
        return self.r.rpop(key)
    def list_lpop(self, key):
        return self.r.lpop(key)
    # 获取所有数据 rrange(key,0,-1)

    def list_getall(self, key,start,end):
        return self.r.lrange(key, start, end)
    # 获取长度  len
    def list_len(self, key):
        return self.r.llen(key)

    # sorted set zset
    def sortedset_add(self, pkey, key, score):
        mapping = {key: score}
        self.r.zadd(pkey, mapping)

    # 获取
    def sortedset_getall(self, pkey):
        return self.r.zrange(pkey, 0, -1)

    # 根据分数的区间获取
    def sortedset_getbyscore(self, pkey, min, max):
        return self.r.zrangebyscore(pkey, min, max)

    # 删除
    def sortedset_delete(self, pkey, key):
        self.r.zrem(pkey, key)

    def hset_add(self,key,filed,value):
        self.r.hset(key,filed,value)

    def hget_get(self,key,filed):
        return self.r.hget(key, filed)


r = Myredis()

# r.list_push('smscode','13213131313')
# r.list_push('smscode', '13213131312')
# r.list_push('smscode','13213131311')

# aa = r.list_getall('smscode')
# print(aa)
# len = r.list_len('smscode')
# print(len)

# bb = r.list_pop('smscode')
# aa = r.list_getall('smscode')
# print(aa)
# print(bb)

